﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using web.data.entities;

namespace web.core.Models
{
    public partial class ShoppingCart
    {
        //MusicStoreEntities storeDB = new MusicStoreEntities();

        public string shoppingCartId { get; set; }
        public const string CartSessionKey = "CartId";

        public static ShoppingCart GetCart(HttpContextBase context)
        {
            var cart = new ShoppingCart();
            cart.shoppingCartId = cart.GetCartId(context);
            return cart;
        }

        // Helper method to simplify shopping cart calls
        public static ShoppingCart GetCart(Controller controller)
        {
            return GetCart(controller.HttpContext);
        }

        public void AddToCart(product product)
        {
            web.bussines.cart_service cartSvc = new bussines.cart_service();
            var cartItem = cartSvc.GetCartByCartIdAndProductID(shoppingCartId, product);

            if (cartItem == null)
            {
                // Create a new cart item
                cartItem = new cart
                {
                    product_id = product.product_id,
                    cart_id = shoppingCartId,
                    count = 1,
                    date_created = DateTime.Now
                };
                //DB AddToCarts(cartItem);
                cartSvc.AddToCart(cartItem);
            }
            else
            {
                // Add one to the quantity
                cartItem.count++;
            }

            //Update to CartItem
            cartSvc.UpdateQuantityCart(cartItem);

        }

        public void UpdateProductInCart(int record_id,int? product_id,int count)
        {
            web.bussines.cart_service cartSvc = new bussines.cart_service();
            var cartItem = new cart
            {
                record_id = record_id,
                product_id = product_id,
                cart_id = shoppingCartId,
                count = count,
                date_created = DateTime.Now
            };
            
            //Update to CartItem
            cartSvc.UpdateQuantityCart(cartItem);

        }

        public void RemoveFromCart(int recordId)
        {
            web.bussines.cart_service cartSvc = new bussines.cart_service();
            //Get the cart
            var cartItem = cartSvc.GetCartByCartIdAndRecordID(shoppingCartId, recordId);

            if (cartItem != null)
            {
                if (cartItem.count > 1)
                {
                    cartItem.count--;
                    //Update to CartItem
                    cartSvc.UpdateQuantityCart(cartItem);
                }
                else
                {
                    //storeDB.Carts.DeleteObject(cartItem);
                    cartSvc.DeleteCartItem(cartItem);
                }

            }
        }

        public void DeleteCartItem(cart cartItem)
        {
            web.bussines.cart_service cartSvc = new bussines.cart_service();
            cartSvc.DeleteCartItem(cartItem);
        }

        //Clear Cart with one shoppingCartID
        public void EmptyCart()
        {
            web.bussines.cart_service cartSvc = new bussines.cart_service();
            var cartItems = cartSvc.GetListCartItemByShoppingCartID(shoppingCartId);

            foreach (var item in cartItems)
            {

                cartSvc.DeleteCartItem(item);
            }
        }

        //List CartItem 
        public List<cart> GetListCartItemByCartID()
        {
            web.bussines.cart_service cartSvc = new bussines.cart_service();
            return cartSvc.GetListCartItemByShoppingCartID(shoppingCartId);
        }

        //TotalValue of CartItem
        public double GetTotalValueCartItem()
        {
            web.bussines.cart_service cartSvc = new bussines.cart_service();
            return cartSvc.GetTotal(shoppingCartId);
        }

        //Count CartItem
        public int GetCountCartItem()
        {
            web.bussines.cart_service cartSvc = new bussines.cart_service();
            return cartSvc.GetCountCartItem(shoppingCartId);
        }

        public int CreateOrder(order order)
        {
            web.bussines.cart_service cartSvc = new bussines.cart_service();
            web.bussines.product_service proSvc = new bussines.product_service();
            double orderTotal = 0;

            var cartItems = cartSvc.GetListCartItemByShoppingCartID(shoppingCartId);
           
            //Iterate the items in the cart, adding Order Details for each
            foreach (var item in cartItems)
            {
                double? _price_value = 0;
                if(item.product_id!=null)
                {
                    _price_value = proSvc.GetProductPriceByID((int)item.product_id);
                }
                var orderDetail = new order_detail()
                {
                    product_id = item.product_id,
                    order_id = order.order_id,
                    quantity = item.count,
                    price_value = _price_value * (item.count??0)
                };
                int orderdetailID = cartSvc.CreateOrderDetail(orderDetail);
            }

            orderTotal = cartSvc.GetTotal(shoppingCartId);
            order.total = orderTotal;
            cartSvc.UpdateTotalOrder(order.order_id, orderTotal);

            ////Empty the shopping cart
            //EmptyCart();

            //Return the OrderId as a confirmation number
            return order.order_id;
        }
       
       
        // We're using HttpContextBase to allow access to cookies.
        public string GetCartId(HttpContextBase context)
        {
            if (context.Session[CartSessionKey] == null)
            {
                if (!string.IsNullOrWhiteSpace(context.User.Identity.Name))
                {
                    context.Session[CartSessionKey] = context.User.Identity.Name;
                }
                else
                {
                    // Generate a new random GUID using System.Guid class
                    Guid tempCartId = Guid.NewGuid();

                    // Send tempCartId back to client as a cookie
                    context.Session[CartSessionKey] = tempCartId.ToString();
                }
            }

            return context.Session[CartSessionKey].ToString();
        }

        // When a user has logged in, migrate their shopping cart to
        // be associated with their username
        public void MigrateCart(string userName)
        {
            web.bussines.cart_service cartSvc = new bussines.cart_service();
            var cartItems = cartSvc.GetListCartItemByShoppingCartID(shoppingCartId);

            foreach (var item in cartItems)
            {
                item.cart_id = userName;
                cartSvc.UpdateCartId(item);
            }
        }

        public static cart GetCartbyRecordID(int recordID)
        {
            web.bussines.cart_service cartSvc = new bussines.cart_service();
            return cartSvc.GetCartByRecordID(recordID);
        }

        public static List<cart> GetListCartItemByShoppingCartID(string shoppingCartId)
        {
            web.bussines.cart_service cartSvc = new bussines.cart_service();
            return cartSvc.GetListCartItemByShoppingCartID(shoppingCartId);
        }

        public static customer GetCustomerByID(int id)
        {
            web.bussines.cart_service cartSvc = new bussines.cart_service();
            return cartSvc.GetCustomerByID(id);
        }

        public static customer GetCustomerByUserID(int id)
        {
            web.bussines.cart_service cartSvc = new bussines.cart_service();
            return cartSvc.GetCustomerByUserID(id);
        }
    }
}